package com.zx.servicelearn.activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.zx.servicelearn.R;
import com.zx.servicelearn.model.Book;
import com.zx.servicelearn.model.User;

import java.util.ArrayList;
import java.util.List;

import static com.zx.servicelearn.provider.BookProvider.BOOK_CONTENT_URI;
import static com.zx.servicelearn.provider.BookProvider.USER_CONTENT_URI;

public class ProviderActivity extends AppCompatActivity {
    TextView result_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provider);
        result_tv = findViewById(R.id.result_tv);
//        getContentResolver().query(BOOK_CONTENT_URI, null, null, null, null);
//        getContentResolver().query(uri, null, null, null, null);
        ContentValues values = new ContentValues();
        values.put("_id", 7);
        values.put("name", "PHP");
        getContentResolver().insert(BOOK_CONTENT_URI, values);//插入

        //查询
        Cursor cursor = getContentResolver().query(BOOK_CONTENT_URI, null, null, null, null);
        List books = new ArrayList();

        try {
            while (cursor.moveToNext()) {
                int _id = cursor.getInt(0);
                String name = cursor.getString(1);
                books.add(new Book(_id, name));
            }
            Log.e("ProviderActivity", books.toString());
        } catch (Exception e) {

        } finally {
            if (cursor != null)
                cursor.close();
        }
        Cursor cursor2 = getContentResolver().query(USER_CONTENT_URI, null, null, null, null);
        List users = new ArrayList();

        try {
            while (cursor2.moveToNext()) {
                int _id = cursor2.getInt(0);
                String name = cursor2.getString(1);
                int sex = cursor2.getInt(2);
                users.add(new User(_id, name, sex));
            }
            Log.e("ProviderActivity", users.toString());
        } catch (Exception e) {

        } finally {
            if (cursor2 != null)
                cursor2.close();
        }

        result_tv.setText("books:" + books.toString() + "\nusers：" + users.toString());
    }
}
